import orderSourceChart from './order-source-chart.vue'
import chargingCategory from './charging-category.vue'
import finishReasonChart from './finish-reason-chart.vue'
import sectionChargeChart from './section-charge-chart.vue'

import orderAmountChart from './order-amount-chart.vue'
import latelyOrderAmountChart from './lately-order-amount-chart.vue'

import orderStatusChart from './order-status-chart.vue'

import latelyChargeCountChart from './lately-charge-count-chart.vue'
import consumePowerChart from './consume-power-chart.vue'

import { Request } from '@/utils'

export default {
    data () {
        return {
            orderSourceData : null, //订单来源
            rechargingCategoryData : null, //计费类型
            finishReasonData : null, //充电结束原因
            sectionChargeData : null, //区间充电时长

            orderAmountData : null, //订单累计金额
            latelyOrderAmountData : null, //近七天订单金额
            


            rechargeTotalAmountData : null, //充值累积金额
            latelyRechargeAmountData : null, //近七日充值金额

            orderStatusData : null, //订单状态

            
            latelyChargeCountData : null, //近七日下单次数
            
            consumePowerData : null, //充电电量
        }
    },

    components : {
        orderSourceChart,
        chargingCategory,
        finishReasonChart,
        sectionChargeChart,

        orderAmountChart,
        latelyOrderAmountChart,

        orderStatusChart,

        latelyChargeCountChart,

        consumePowerChart
    },

    methods : {
        run () {
            (new Request()).get('statistic/order/totalOrderType', res => { //订单来源
                var sourceType = {1 : '微信', 2 : '投币', 3 : '刷卡', 4 : '支付宝'}
                var orderSourceData = []
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(res => {
                        var data = {
                            name : sourceType[res.type],
                            value : res.orderNum
                        }
                        orderSourceData.push(data)
                    })
                }

                this.orderSourceData = orderSourceData
            });



            (new Request()).get('statistic/order/totalChargeType', res => { //计费类型
                var sourceType = {1 : '计时', 2 : '计量'}
                var rechargingCategoryData = []
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(res => {
                        var data = {
                            name : sourceType[res.type],
                            value : res.orderNum,
                            type : res.type
                        }
                        rechargingCategoryData.push(data)
                    })
                }

                this.rechargingCategoryData = rechargingCategoryData
            });

            (new Request()).get('statistic/order/totalChargeResult', res => { //充电结束原因
                var reasonType = {1 : '时间跑完', 2 : '正常充满', 3 : '系统异常', 4 : '其它'}
                var finishReasonData = []
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(res => {
                        var data = {
                            name : reasonType[res.chargeResult],
                            value : res.num,
                            type : res.chargeResult
                        }
                        finishReasonData.push(data)
                    })
                }
                this.finishReasonData = finishReasonData
            });

            (new Request()).get('statistic/order/totalTodayHourInterval', res => { //区间充电时长
                var sectionChargeData = {
                    date : [],
                    data : []
                }
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        sectionChargeData.date.push(item.hourStr)
                        sectionChargeData.data.push(item.num || 0)
                    })
                }
                this.sectionChargeData = sectionChargeData
            });


            

            (new Request()).get('statistic/order/totalOrderAmountType', res => { //订单累计金额
                try {
                    var orderAmountData = [
                        {name : '微信', value : 0},
                        {name : '支付宝', value : 0},
                        {name : '平台', value : 0}
                    ]
    
                    if(res.success && res.data) {
                        res.data.forEach(item => {
                            var index = 0
                            switch(parseInt(item.types)) {
                                case 1 : index = 0
                                    break
                                case 2 : index = 1
                                    break
                                default : index = 2
                            }
    
                            orderAmountData[index].value += (item.amount * 1)
                        })
                    }
    
                    this.orderAmountData = orderAmountData
                } catch (error) {
                }
            });

            (new Request()).get('statistic/order/totalSevenDaysChargeOrderNum', res => { //近七日订单金额
                var latelyOrderAmountData = {
                    date : [],
                    data : [
                        [], //订单金额
                        [] //实际消费金额
                    ]
                }

                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        latelyOrderAmountData.date.push(item.date)
                        latelyOrderAmountData.data[0].push(item.amount || 0)
                        latelyOrderAmountData.data[1].push(item.consumeAmount || 0)
                    })
                }

                this.latelyOrderAmountData = latelyOrderAmountData
    
            });


            (new Request()).get('statistic/order/totalChargeOrderStatus', res => { //订单状态
                var statusList = {1 : '充电结束', 2 : '充电失败', 3 : '占位', 4 : '储物'}
                var orderStatusData = []

                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        orderStatusData.push({name : statusList[item.state], value : (item.num / item.totalNum).toFixed(2)})
                    })
                }

                this.orderStatusData = orderStatusData
    
            });

            


            (new Request()).get('statistic/order/totalChargeOrderTypeNum', res => { //近七日下单次数
                var latelyChargeCountData = {
                    date : [],
                    wechat : [],
                    alipay : [],
                    platform : []
                }
                if(res.success && res.data) {
                    res.data.forEach(item => {
                        latelyChargeCountData.date.push(item.date)
                        latelyChargeCountData.wechat.push(item.wechat || 0)
                        latelyChargeCountData.alipay.push(item.alipay || 0)
                        latelyChargeCountData.platform.push(item.platform || 0)
                    })
                    
                }

                this.latelyChargeCountData = latelyChargeCountData
            });
            

            (new Request()).get('statistic/order/totalSevenDaysChargeEnergy', res => { //充电电量
                var consumePowerData = { //
                    date : [],
                    data : []
                }

                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        consumePowerData.date.push(item.date)
                        consumePowerData.data.push(item.consumeEnergy || 0)
                    })
                }

                this.consumePowerData = consumePowerData
    
            });

            setTimeout(() => {
                this.run()
            }, 30 * 60 * 1000)
        }
    },

    mounted () {
        this.run()
    }
}